package com.ruoyi.mateproduction.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mateproduction.domain.MateProductionPlan;
import com.ruoyi.mateproduction.service.MtProductionService;
import com.ruoyi.material.domain.MaterialProduct;
import com.ruoyi.product.domain.ProductModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author
 * @Version 1.0
 */
@RestController
@RequestMapping("/mateProduction")
public class MtProductionPlanController extends BaseController {
    @Autowired
    MtProductionService mtProductionService;

    /**
     * 保存生产计划
     */
    @Log(title = "生产计划", businessType = BusinessType.INSERT)
    @PostMapping("/addMatePlan")
    public AjaxResult addCheck(@RequestBody MateProductionPlan mateProductionPlan) {
        mateProductionPlan.setDeptId(SecurityUtils.getLoginUser().getCompanyId());
        int num = mtProductionService.addMatePlan(mateProductionPlan);
        if (num > 0) {
            return AjaxResult.success("添加成功");
        } else {
            return AjaxResult.error("添加失败");
        }
    }

    /**
     * 查询生产计划页面
     */
    @GetMapping("/matePlanList")
    @ResponseBody
    public TableDataInfo matePlanList(@RequestParam(value = "beginTime", required = false) String beginTime,
                                   @RequestParam(value = "endTime", required = false) String endTime) {
        MateProductionPlan mateProductionPlan = new MateProductionPlan();
        mateProductionPlan.setBeginTime(beginTime);
        mateProductionPlan.setEndTime(endTime);
        startPage();
        mateProductionPlan.setDeptId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        List<MateProductionPlan> list = mtProductionService.matePlanList(mateProductionPlan);
        return getDataTable(list);
    }

    @GetMapping("/selectProductModelLists")
    @ResponseBody
    public TableDataInfo selectProductModelLists(
                                                 @RequestParam(value = "mateProductName", required = false) String mateProductName,
                                                 @RequestParam(value = "mateProductType", required = false) String mateProductType) {
        startPage();
        Long deptId = SecurityUtils.getLoginUser().getUser().getCompanyId();
        MaterialProduct product = new MaterialProduct();
        product.setCompanyId(deptId);
        product.setMateProductName(mateProductName);
        product.setMateProductType(mateProductType);
        return getDataTable(mtProductionService.selectProductLists(product));
    }


    /**
     * 查询自己公司旗下自己生产的产品
     */
    @GetMapping("/tableSelect")
    @ResponseBody
    public JSONObject tableSelect(@RequestParam(value = "productType", required = false) String productType) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("msg", "");
        HashMap map = new HashMap();
        map.put("deptId", SecurityUtils.getLoginUser().getUser().getCompanyId());
        if (productType != null & !"".equals(productType)) {
            map.put("productType", productType);
        }
        List<ProductModel> productModels = mtProductionService.selectTableList(map);
        jsonObject.put("count", productModels.size());
        JSONArray jsonArray = new JSONArray();
        Long amount = 0L;
        for (int i = 0; i < productModels.size(); i++) {
            ProductModel productModel = productModels.get(i);
            if (productModel.getSalesUnit().equals("盒")) {
                String b = productModel.getWarehousingNum();
                int c = Integer.valueOf(b);
                amount = c / productModel.getFactor();
            } else {
                amount = Long.valueOf(productModel.getWarehousingNum());
            }

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("modelCode", productModel.getModelCode());
            jsonObject1.put("productId", productModel.getProduct().getProductId());
            jsonObject1.put("productName", productModel.getProduct().getProductName());
            jsonObject1.put("productType", productModel.getProductType());
            jsonObject1.put("unit", productModel.getSalesUnit());
            jsonObject1.put("manufacturerName", productModel.getManufacturerName());
            jsonObject1.put("warehousingNum", amount);
            jsonObject1.put("amount", productModel.getAmount());
            jsonObject1.put("factor", productModel.getFactor());
            jsonObject1.put("scopeId", productModel.getScopeId());
            jsonArray.add(jsonObject1);
        }

        jsonObject.put("data", jsonArray);
        return jsonObject;
    }

    /**
     * 销售订单管理list
     */
    @GetMapping("/productList")
    @ResponseBody
    public TableDataInfo productList(@RequestParam(value = "productName", required = false) String productName,
                                     @RequestParam(value = "productType", required = false) String productType) {
        startPage();
        Map<String, Object> map = new HashMap<>();
        map.put("deptId", SecurityUtils.getLoginUser().getUser().getCompanyId());
        if (productName != null) {
            map.put("productName", productName);
        }
        if (productType != null) {
            map.put("productType", productType);
        }
        List<ProductModel> list = mtProductionService.selectProductList(map);
        return getDataTable(list);
    }

    /*
    查询已经做完的生产计划
     */
    @GetMapping("/selectPlan")
    public MateProductionPlan selectPlan(@RequestParam(value = "planId", required = false) Long planId) {
        MateProductionPlan mateProductionPlan = mtProductionService.selectPlan(planId);
        return mateProductionPlan;
    }

    /**
     * 删除生产计划
     */
    @Log(title = "删除生产计划", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{planId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("planId") Long planId) {
        mtProductionService.removePlan(planId);
        return AjaxResult.success("删除成功");
    }

    /*
    编辑生产计划
     */
    @PostMapping("/editPlan")
    @ResponseBody
    public AjaxResult editPlan(@RequestBody MateProductionPlan mateProductionPlan) {
        int num = mtProductionService.editPlan(mateProductionPlan);
        if (num > 0) {
            return AjaxResult.success("修改成功");
        } else {
            return AjaxResult.error("修改失败");
        }
    }


}
